<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	$id=$_RQ['id'];
	$merchid=$_RQ['merchid'];
	if($merchid){
		$id=$this->model('worker')->checkadd($merchid);
	}
	$item=pdo_get('jy_aikefu_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['images']=json_decode($item['images'],true);
	$item['onlinetime']=json_decode($item['onlinetime'],true);
	$item['priceset']=json_decode($item['priceset'],true);
	$item['param']=json_decode($item['param'],true);
	$item['zixuns']=json_decode($item['zixuns'],true);
	$item['peixuns']=json_decode($item['peixuns'],true);
	$item['tags']=json_decode($item['tags'],true);
	$item['api_data']=json_decode($item['api_data'],true);
	$item['salenum']=pdo_count('jy_aikefu_tcp',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$item['id']));
	//来自海报
	$jy_poster = $this->plugin_exists('jy_poster');
	if($jy_poster  && $return['setting']['no_poster']!=1){
		$item['isposter']=1;
	}
	$jy_feedback = $this->plugin_exists('jy_feedback');
	if($jy_feedback){
		$return['jy_feedback']=1;
	}
	$item['showprice']=$item['priceset'][0]['price'];
	$item['logo']=tomedia($item['logo']);
	if(!$item['fee_type']){
		$item['fee_type']=$this->setting['fee_type'];
		$item['fee_dui']=$this->setting['fee_dui'];
		$item['fee_duibi']=$item['fee_dui']*$this->setting['jifen_rate'];
		$item['fee_ju']=$this->setting['fee_ju'];
	}
	$return['goods']=$item;
	$return['post']['goodsid']=$item['id'];
	$return['post']['pricekey']=0;
	$return['zxout_time']=plugin('jy_weishop')->model('store')->zxout_time();
	$return['xieyi']=$this->plugin_setting('xieyi');
	$return['goods']['imagetop']=$return['goods']['images'][0]==$return['goods']['image']?$return['goods']['images'][1]:$return['goods']['images'][0];
	$return['showtype']=0;
	if($return['pluginset']['ismerch_btn'] && $item['merchid']){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),array('id','lat_lng','pcd','address'));
		$merch['lat_lng']=json_decode($merch['lat_lng'],true);
		$merch['pcd']=json_decode($merch['pcd'],true);
		$orderby='id desc';
		$psize = 3;
		$pindex = max(1, intval($_RQ['page']));
		$jy_sort = $this->plugin_exists('jy_sort');
		if($jy_sort){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'mid'=>$item['mid']);
			$return['list5']=pdo_getslice('jy_sort_item',$params,array($pindex,$psize),$total,$fields,'',$orderby);
			foreach($return['list5'] as &$item){
				$item['createtime']=date('Y-m-d H:m:s',$item['createtime']);
				$item['images']=json_decode($item['images'],true);
			}
		}
		$jy_shopbaojia = $this->plugin_exists('jy_shopbaojia');
		if($jy_shopbaojia){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0,'merchid'=>$item['merchid']);
			$return['list6']=pdo_getslice('jy_shopbaojia_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
			foreach($return['list6'] as &$item){
				$item['updatetime']=$item['updatetime']?:$item['createtime'];
				$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
				$item['merch']=$merch;
				$item['likes']=pdo_count('jy_shopbaojia_shoucang',array('listid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'isshoucang'=>1),'id');
				$item['typetitle']=$this->content_types[$item['isnews']];
			}
		}
		$jy_xiaoemerch = $this->plugin_exists('jy_xiaoemerch');
		if($jy_xiaoemerch){
			$fields=array('id','title','type','desc','thumb','userid','group_id','isjie','star','readnum','createtime');
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0,'merchid'=>$item['merchid']);
			$return['list7']=pdo_getslice('jy_xiaoeknow_video',$params,array($pindex,$psize),$total,$fields,'',$orderby);
			foreach($return['list7'] as &$item){
				$item['updatetime']=$item['updatetime']?:$item['createtime'];
				$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
				$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
				$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
				$item['thumb']=tomedia($item['thumb']);
				$lasttime=TIMESTAMP-$item['updatetime'];
				if($lasttime>24*3600){
					$item['updatetimetitle']=intval($lasttime/24*3600).'天前';
				}elseif($lasttime>=3600){
					$item['updatetimetitle']=intval($lasttime/3600).'小时前';
				}else{
					$item['updatetimetitle']=intval($lasttime/60).'分钟前';
				}
				$video_log=pdo_get('jy_xiaoeknow_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));
				$item['video_log']=$video_log;
				$item['likes']=pdo_count('jy_xiaoeknow_video_log',array('video_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'islike'=>1),'id');
				$item['typetitle']=$this->content_types[$item['isnews']];
			}
		}
		$fields=array('id','price','addprice','ispayadd','createtime','ispay','mid','paytime','status','buytype','sendtype','ordersn','addressid','dispatchprice','plugin_name');
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'merchid'=>$item['merchid']);
		$return['list8']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
		foreach($return['list8'] as &$item){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
			foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
				//商品规格
				if($gv['optionid']){
					$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
					$goodsinfo['selespename']=implode("，",$specs);
				}
				$gv=$goodsinfo;
			}
			$item['goods']=$ordergoods;
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		}
	}
	$return['sharedata']['link']=custom_url('jy_aikefu/goods.detail//id='.$return['goods']['id'].'&fromuser='.$_SESSION['uid'],true);
	$keywords=$return['goods']['share_keywords']?:$return['goods']['title'].','.$return['sharedata']['desc'];
	$desc=$return['sharedata']['desc'];
	set_html('title',$return['goods']['title']?$return['goods']['title'].'-'.$return['shopset']['title']:'客服详情');
	set_html('keywords',$keywords);
	set_html('description',$desc);
	exi($return);
}
if($_RQ['op']=='upposter'){
	$id=intval($_RQ['id']);
	$url=custom_url('jy_aikefu/goods.detail//id='.$id.'&frommid='.$_SESSION['uid'],true);
	$qr=cfc('tools')->createqrcode($url,'250',true);
	$qr=tomedia($qr);
	$image='oss/'.$_SESSION['uniacid'].'/images/jy_aikefu/id'.$id.'.jpg';
	$isadd=cfc('files')->source_check($image);
	if(!$isadd && $_SESSION['wxapp_id']){
		$wxappurl='/jy_aikefu/songget/index?id='.$id.'&frommid='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($wxappurl,$wxapp['appid'],$wxapp['appsecret'],200);
		$path='oss/'.$_SESSION['uniacid'].'/images/jy_aikefu/';
		if(!file_exists(ROOT_AT.$path)){
			mkdir(ROOT_AT.$path,0700, true);
		}
		$filename=$path.'/id'.$id.'.jpg';
		$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$wxappurl);
	}
	$worker=pdo_get('jy_aikefu_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$posterset=$this->plugin_setting('poster');
	$posterset['data']['height']=300;
	foreach($posterset['data']['data'] as &$dv){
		if($dv['type']=='title'){
			$dv['type']='text';
			$dv['value']=$worker['title'];
		}
		if($dv['type']=='desc'){
			$dv['type']='text';
			$dv['value']=$worker['subtitle'];
		}
	}
	$poster=plugin('jy_poster')->get_active('','',$id,$openid,$qr,$posterset['data'],'jy_aikefu');
	$poster['posterimg']=$poster['poster'];
	exi($poster);
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=$this->model('goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='check_buy'){
	$postdata=get_postdata();
	$type = $_RQ['type'];
	$goodsid = $_RQ['goodsid'];
	//$buyxieyi=$this->plugin_setting('xieyi');
	if(!$postdata['checked'] && $buyxieyi['isopen']){
		//	exi('请先阅读协议！','error');
	} 
	if(!$goodsid){
		exi('请选择客服！','error');
	}
	$item=pdo_get('jy_aikefu_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
	if(!$type){
		if(!$item['fee_type']){
			$item['fee_type']=$this->setting['fee_type'];
			$item['fee_dui']=$this->setting['fee_dui'];
			$item['fee_ju']=$this->setting['fee_ju'];
		}
		$price=$item['fee_dui'];
		$goodsinfo=$item['title'].'-在线咨询';
	}elseif($type==1){
		$price=$item['mobileprice'];
		$goodsinfo=$item['title'].'-电话咨询';
	}
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'type'=>$type,
			'workerid'=>$item['id'],
			'groupid'=>$item['groupid'],
			'price'=>$price,
			'goodsinfo'=>$goodsinfo,
			'ordersn'=>get_ordersn('jy_aikefu_tcp','ordersn'),
			'createtime'=>TIMESTAMP,
	);
		pdo_insert('jy_aikefu_tcp',$insert);
		$id=pdo_insertid();
		if($price>0){
			$params=array(
					'id'=>$id,
					'title'=>'对话订单',
					'fee'=>$price,
					'plugin'=>'jy_aikefu',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_aikefu/goods.detail/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_aikefu/mytcp/',
					'error_url'=>'jy_aikefu/mytcp/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('tcp',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
		}else{
			$payresult=array('fee'=>0);
			$order=$insert;
			$order['id']=$id;
			$this->model('model')->payresult($order);
			$this->model('notice')->notice_fuwuorder($order);	
			exi('提交成功！','','jy_aikefu/mytcp/');
		}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_aikefu_tcp',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$this->model('model')->payresult($order,$payresult,1);
	$item=pdo_get('jy_aikefu_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['workerid']));
	if(!$item['fee_type']){
		$item['fee_type']=$this->setting['fee_type'];
		$item['fee_dui']=$this->setting['fee_dui'];
		$item['fee_duijiang']=$this->setting['fee_duijiang'];
	}
	$item['fee']=$item['fee_duijiang'];
	$this->model('worker')->upfee($item);
	$this->model('notice')->notice_fuwuorder($order);	
	exi('支付成功','success','jy_aikefu_tcp/tcp/list/status=1&id='.$order['id']);
}